
import { Component, OnInit, ElementRef, AfterViewInit, ViewChild } from '@angular/core';
import { DxDataGridComponent } from 'devextreme-angular';
import 'rxjs/add/operator/map';
import { Subject } from 'rxjs/Rx';
import { Observable } from 'rxjs/Rx';
import { CommService } from '../../comm/comm.service';
import { ReportUrls } from '../../comm/report.urls.conf';
import { SubArea } from '../../model/report/sub-area';
import { Resource } from '../../model/report/resource';
import { QueryModel } from '../../model/report/query-model';
import { ResponseModel } from '../../model/comm/response-model';
import { ChartOptionCreater } from '../../model/comm/chart-option-creater';
import { Shift } from '../../model/report/shift';
import 'rxjs/add/operator/toPromise';
declare const $: any;
import * as moment from 'moment';
import * as Enumerable from 'linq';
import { TVResource } from 'app/ptpmc/model/comm/tv-resource';
import { DxDropDownBoxComponent } from 'devextreme-angular';
import { TipPtrSummary } from 'app/ptpmc/model/report/tip-ptr-summary';

@Component({
    selector: 'app-report-tipcalc',
    templateUrl: './tipcalc.component.html',
    styleUrls: ['./tipcalc.component.scss']
})

export class TipCalcComponent implements OnInit {
    title = 'TIPCalculation';
    public ctsList: TipPtrSummary[];
    /*生产线 - 班次*/
    public subAreaItems: SubArea[];
    public shiftItems: any[];
    public equipemntItems: TVResource[] = [];
    public subAareSelected: any;
    public shiftSelected: any;
    public equipmentSelected: TVResource[];
    constructor(private commServices: CommService) {
    }

    ngOnInit() {
        this.commServices.initDatePicker($('#DateTimeRange'), 'YYYY-MM-DD HH:mm');
        this.shiftItems = this.commServices.shifts;
        this.shiftSelected = this.shiftItems[0];
        this.subAreaItems = this.commServices.getSubAreas();
        this.subAareSelected = this.subAreaItems[0];
        this.equipemntItems = this.commServices.getTVResourceBySubArea(this.subAreaItems[0].subArea);
        this.equipmentSelected = this.equipemntItems;
    }

    @ViewChild('dddb') dddb: DxDropDownBoxComponent;
    clearEquipmentSelected() {
        this.equipmentSelected = [];
    }
    equipmentSelectedOk() {
        this.dddb.instance.close();
    }

    onSubAreaSelectedChange(subArea: any): void {
        this.equipemntItems = this.commServices.getTVResourceBySubArea(subArea.value.subArea);
        this.equipmentSelected = this.equipemntItems;
    }
    /**
     * 获取表格数据
     */
    getReportData(): void {
        const queryModel: QueryModel = new QueryModel();
        queryModel.dateTimeRange = this.getTime('DateTimeRange');
        queryModel.querySubAreaIds = [this.subAareSelected.subArea];
        queryModel.queryResourceIds = this.equipmentSelected.map(e => e.resource);
        queryModel.shift = this.shiftSelected.shiftId;
        debugger
        this.commServices
            .getList(queryModel, ReportUrls.TipPtrSummaryCalc)
            .subscribe(model => {
                if (model.resultCode === 200) {
                    debugger
                    model.data.forEach(e => {
                        if(queryModel.querySubAreaIds[0].slice(0,2) === 'GF'){
                            var sub: string = '';
                            sub = e.subArea.replace(/(^\s*)|(\s*$)/g, "");
                            switch(sub){
                                case 'GFAL1': sub += '_装配主线';  break;
                                case 'GFBU1': sub += '_测试线';  break;
                                case 'GFCL1': sub += '_CASE 机加工线';  break;
                                case 'GFCR1': sub += '_行星齿轮组分装线';  break;
                                case 'GFCS1': sub += '_中间支撑架分装线';  break;
                                case 'GFDH1': sub += '_飞轮盘减震器壳体分装线';  break;
                                case 'GFHL1': sub += '_HOUSING 机加工线';  break;
                                case 'GFTC1': sub += '_壳体分装线';  break;
                                case 'GFTD1': sub += '_中间轴&差速器分装线';  break;
                            }
                            e.subArea = sub;
                        }
                        e.goodProcess = e.totCt - e.rjParts * (e.totCt / e.totCt);
                        e.scrapProcess = e.rjParts * (e.totCt / e.numNcyc);
                        e.totalWorktime = e.totFt + e.totPt;
                        e.avgCycle = e.totCt / e.totCt;
                        e.downMTTR = e.numFlt === 0 ? 0 : e.totFt / e.numFlt;
                        e.downMCBF = e.numFlt === 0 ? 0 : e.numNcyc / e.numFlt;
                        e.saa = e.totCt / (e.totCt + e.totFt);
                        e.grossJPH = e.numNcyc / e.totCt;
                        e.avgDown = e.numFlt === 0 ? 0 : e.totFt / e.numFlt;
                        e.blockedStarved = e.totBt + e.totSt;
                        /*
                        //e.targetWorktime = ((3600 * 开动率 / JPH) * 目标产量) / 60;
                        // e.cycleDiff =  规划节拍 - 平均节拍
                        // e.purchCycle = (3600 * 开动率 / JPH) * 平行工位数量
                        // e.netJPH = (单机开动率(e.saa) * 3600 / 平均节拍(avgCycle) ) * (1 - 不合格产量 / 循环次数)
                        // e.purJPH = JPH / 平行工位数量;
                        // e.jphDiff = 考虑开动率的实际JPH - 规划JPH
                        // e.avgBLK = e.totBt / 堵塞次数 (gdParts + rjParts);
                        // e.avgSTV = e.totSt / 待料次数 (gdParts + rjParts)
                        */
                    });

                    this.ctsList = model.data;

                    // goodProcess: number;// (min)(合格品的加工时间)  总工作循环时间 - 不合格品 * (总工作循环时间 / 循环次数)
                    // scrapProcess: number;// (min)(不合格品的加工时间) //不合格品 * (总工作循环时间 / 循环次数)
                    // targetWorktime: number;// (min)(理论工作时间)  ((3600 * 开动率 / JPH) * 目标产量) / 60
                    // totalWorktime: number;// ( (min)(总工作时间)  自动运行时间 + 停机时间
                    // purchCycle: number;// (sec)(规划节拍)  (3600 * 开动率 / JPH) * 平行工位数量
                    // avgCycle: number;// (sec)(实际平均节拍)  总工作循环时间 / 循环次数
                    // cycleDiff: number;// (sec)(规划节拍 - 实际平均节拍)   规划节拍 - 平均节拍
                    // downMTTR: number;// (平均停机时间)  停机时间 / 停机次数
                    // downMCBF: number;// ( (平均停机频次)  循环次数 / 停机次数
                    // saa: number;// (单机开动率)  总工作循环时间 / (总工作循环时间 + 停机时间)
                    // grossJPH: number;//  (不考虑开动的实际JPH)  循环次数 / 总工作循环时间
                    // netJPH: number;// (考虑开动率的实际JPH)   (单机开动率 * 3600 / 平均节拍 ) * (1 - 不合格产量 / 循环次数)
                    // purJPH: number;// (规划JPH)  规划JPH / 平行工位数量
                    // jphDiff: number;// (Net JPH - Pur JPH)   考虑开动率的实际JPH - 规划JPH
                    // avgDown: number;// (sec)(平均停机时间)    停机时间 / 停机次数
                    // avgBLK: number;//  (sec)(平均堵塞时间)    堵塞时间 / 堵塞次数
                    // avgSTV: number;// (sec)(平均待料时间)   待料时间 / 待料次数
                    // blockedStarved: number;// (min)(堵塞待料时间)    堵塞时间 + 待料时间
                } else {
                    console.log(model.resultCode + ',' + model.resultMessage);
                }
            });
    }

    getTime(id: any): String {
        const timerange: String = $('#' + id).val();
        return timerange;
    }

    onExporting(e) {
        e.fileName = e.fileName + new Date().toISOString().replace(/[\-\:\.]/g, '').split('T')[0];
    }
}
